package com.woniu40.petai_user.interceptors;

import com.woniu40.petai_user.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
        if(servletPath.contains("login") || servletPath.contains("reg")){
            return true;
        }

        User user = (User) request.getSession().getAttribute("user");
        if(user != null){
            return true;
        }else {
            request.setAttribute("ErrorResult","请先登录!!!");
            request.getRequestDispatcher("/login").forward(request,response);
            return false;
        }
    }
}
